

//验证是否为数字
function reg_Num(str) {
	if (str.length == 0) {
		return false;
	}
	var Letters = "1234567890.";
	
	for (i = 0; i < str.length; i++) {
		var CheckChar = str.charAt(i);
		if (Letters.indexOf(CheckChar) == -1) {
			return false;
		}
	}
	return true;
}

//得到利率
function getlilv(lilv_class, type, years) {
	var lilv_class = parseInt(lilv_class);
	if (years <= 5) {
		return lilv_array[lilv_class][type][5];
	} else {
		return lilv_array[lilv_class][type][10];
	}
}

//本金还款的月还款额(参数: 年利率 / 贷款总额 / 贷款总月份 / 贷款当前月0～length-1)
function getMonthMoney2(lilv, total, month, cur_month) {
	var lilv_month = lilv / 12; //月利率
	//return total * lilv_month * Math.pow(1 + lilv_month, month) / ( Math.pow(1 + lilv_month, month) -1 );
	var benjin_money = total / month;
	return (total - benjin_money * cur_month) * lilv_month + benjin_money;
}

//本息还款的月还款额(参数: 年利率/贷款总额/贷款总月份)
function getMonthMoney1(lilv, total, month) {
	var lilv_month = lilv / 12; //月利率
	return total * lilv_month * Math.pow(1 + lilv_month, month) / (Math.pow(1 + lilv_month, month) - 1);
}

//计算
function calculate(){	
	var priceTotal = parseFloat( $('#priceTotal').val() ) * 1000;			//贷款总额
	var mortgagePercent = parseInt( $('#mortgagePercent').val() );			//按揭比例
	var mortgageYear = parseInt(  $('#mortgageYear').val()	)				//还款年数
	var interestRates  = 0.0705;
	
	if($('#category input:checked').val() === '2'){
		interestRates  = 0.049;
	}
	
	//贷款总额
	var daikuan_total = priceTotal * mortgagePercent / 10;		
	//还款总额
	var repayment_total = daikuan_total + daikuan_total * interestRates * mortgageYear ;
	//支付利息总额
	var accrual_total = repayment_total - daikuan_total;								
	//贷款月数
	var month_loan = mortgageYear * 12;														
	//每月还款数	
	var month_money = repayment_total / month_loan;
	//首月还款
	var money_first = month_money;			
	
	//1.本金还款
	//月还款
	var all_total2 = 0;
	var month_money2 = "";
	for (j = 0; j < month_loan; j++) {
		//调用函数计算: 本金月还款额
		huankuan = getMonthMoney2(interestRates, daikuan_total, month_loan, j);
		all_total2 += huankuan;
		huankuan = Math.round(huankuan * 100) / 100;
		month_money2 += (j + 1) + "月," + huankuan + "(元)\n";
	}
	//2.本息还款
	
	//月均还款
	month_money = getMonthMoney1(interestRates, daikuan_total, month_loan); //调用函数计算
	//还款总额
	repayment_total = month_money * month_loan;
	//支付利息款
	accrual_total = repayment_total - daikuan_total ;
	
	

	$('#daikuan_total').val(daikuan_total);									//贷款总额
	$('#repayment_total').val(Math.round(repayment_total * 100) / 100);		//还款总额
	$('#accrual_total').val(Math.round(accrual_total * 100) / 100);									//支付利息总额
	$('#month_loan').val(month_loan);										//贷款月数
	$('#month_money').val(Math.round(month_money * 100) / 100);										//每月还款数
	$('#money_first').val(Math.round(month_money * 100) / 100);										//首月还款

	
}

//重置
function reset(){
	$('#daikuan_total').val('');		//贷款总额
	$('#repayment_total').val('');	//还款总额
	$('#accrual_total').val('');		//支付利息总额
	$('#money_first').val('');			//首月还款
	$('#month_loan').val('');				//贷款月数
	$('#month_money').val('');			//每月还款数
	
}

function ext_total(fmobj) {
	//var fmobj = document.calc1;
	//先清空月还款数下拉框
	while ((k = fmobj.month_money2.length - 1) >= 0) {
		fmobj.month_money2.options.remove(k);
	}
	var years = fmobj.years.value;
	var month = fmobj.years.value * 12;

	fmobj.month1.value = month + "(月)";
	fmobj.month2.value = month + "(月)";
	if(fmobj.type.value == 3 ) {
		//--  组合型贷款(组合型贷款的计算，只和商业贷款额、和公积金贷款额有关，和按贷款总额计算无关)
		if(!reg_Num(fmobj.total_sy.value)) {
			alert("混合型贷款请填写商贷比例");
			fmobj.total_sy.focus();
			return false;
		}
		if(!reg_Num(fmobj.total_gjj.value)) {
			alert("混合型贷款请填写公积金比例");
			fmobj.total_gjj.focus();
			return false;
		}
		if(fmobj.total_sy.value == null) {
			fmobj.total_sy.value = 0;
		}
		if(fmobj.total_gjj.value == null) {
			fmobj.total_gjj.value = 0;
		}
		var total_sy = fmobj.total_sy.value;
		var total_gjj = fmobj.total_gjj.value;
		$('#fangkuan_total1').val = "略";//房款总额
		fmobj.fangkuan_total2.value = "略";//房款总额
		fmobj.money_first1.value = 0;//首期付款
		fmobj.money_first2.value = 0;//首期付款

		//贷款总额
		var total_sy = parseInt(fmobj.total_sy.value);
		var total_gjj = parseInt(fmobj.total_gjj.value);
		var daikuan_total = total_sy + total_gjj;
		fmobj.daikuan_total1.value = daikuan_total;
		fmobj.daikuan_total2.value = daikuan_total;

		//月还款
		var lilv_sd = getlilv(fmobj.lilv.value,1, years);//得到商贷利率
		var lilv_gjj = getlilv(fmobj.lilv.value,2, years);//得到公积金利率

		//1.本金还款
		//月还款
		var all_total2 = 0;
		var month_money2 = "";
		for(j = 0; j < month; j++) {
			//调用函数计算: 本金月还款额
			huankuan = getMonthMoney2(lilv_sd, total_sy, month, j) + getMonthMoney2(lilv_gjj, total_gjj, month, j);
			all_total2 += huankuan;
			huankuan = Math.round(huankuan * 100) / 100;
			//fmobj.month_money2.options[j] = new Option( (j + 1) +"月," + huankuan + "(元)", huankuan);
			month_money2 += (j + 1) + "月," + huankuan + "(元)\n";
		}
		fmobj.month_money2.value = month_money2;
		//还款总额
		fmobj.all_total2.value = Math.round(all_total2 * 100) / 100;
		//支付利息款
		fmobj.accrual2.value = Math.round((all_total2 - daikuan_total) * 100) / 100;

		//2.本息还款
		//月均还款
		var month_money1 = getMonthMoney1(lilv_sd, total_sy, month) + getMonthMoney1(lilv_gjj, total_gjj, month);//调用函数计算
		fmobj.month_money1.value = Math.round(month_money1 * 100) / 100 + "(元)";
		//还款总额
		var all_total1 = month_money1 * month;
		fmobj.all_total1.value = Math.round(all_total1 * 100) / 100;
		//支付利息款
		fmobj.accrual1.value = Math.round((all_total1 - daikuan_total) * 100) / 100;
	} 
	else {
		//--  商业贷款、公积金贷款
		var lilv = getlilv(fmobj.lilv.value, fmobj.type.value, fmobj.years.value);//得到利率
		if(false) {//------------ 根据单价面积计算
			if(!reg_Num(fmobj.price.value)) {
				alert("请填写单价");
				fmobj.price.focus();
				return;
			}
			if(!reg_Num(fmobj.sqm.value)) {
				alert("请填写面积");
				fmobj.sqm.focus();
				return;
			}

			//房款总额
			var fangkuan_total = fmobj.price.value * fmobj.sqm.value;
			$('#fangkuan_total1').val = fangkuan_total;
			fmobj.fangkuan_total2.value = fangkuan_total;
			//贷款总额
			var daikuan_total = (fmobj.price.value * fmobj.sqm.value) * (fmobj.anjie.value / 10);
			fmobj.daikuan_total1.value = daikuan_total;
			fmobj.daikuan_total2.value = daikuan_total;
			//首期付款
			var money_first = fangkuan_total - daikuan_total;
			fmobj.money_first1.value = money_first
			fmobj.money_first2.value = money_first;
		} 
		else {


			//房款总额
			$('#fangkuan_total1').val("略");
			$('#fangkuan_total2').val("略");
			//贷款总额
			var daikuan_total = fmobj.daikuan_total000.value * 10000;
			fmobj.daikuan_total1.value = daikuan_total;
			fmobj.daikuan_total2.value = daikuan_total;
			//首期付款
			fmobj.money_first1.value = 0;
			fmobj.money_first2.value = 0;
		}
		//1.本金还款
		//月还款
		var all_total2 = 0;
		var month_money2 = "";
		for(j = 0; j < month; j++) {
			//调用函数计算: 本金月还款额
			huankuan = getMonthMoney2(lilv, daikuan_total, month, j);
			all_total2 += huankuan;
			huankuan = Math.round(huankuan * 100) / 100;
			//fmobj.month_money2.options[j] = new Option( (j + 1) +"月," + huankuan + "(元)", huankuan);
			month_money2 += (j + 1) + "月," + huankuan + "(元)\n";
		}
		fmobj.month_money2.value = month_money2;
		//还款总额
		fmobj.all_total2.value = Math.round(all_total2 * 100) / 100;
		//支付利息款
		fmobj.accrual2.value = Math.round( (all_total2 - daikuan_total) * 100) / 100;
		//2.本息还款
		//月均还款
		var month_money1 = getMonthMoney1(lilv, daikuan_total, month);//调用函数计算
		fmobj.month_money1.value = Math.round(month_money1 * 100) / 100 + "(元)";
		//还款总额
		var all_total1 = month_money1 * month;
		fmobj.all_total1.value = Math.round(all_total1 * 100) / 100;
		//支付利息款
		fmobj.accrual1.value = Math.round( (all_total1 - daikuan_total) * 100) / 100;
	}
	if($("dengeben2").checked) {
		$('#fangkuan_total1').val = fmobj.fangkuan_total2.value;
		fmobj.daikuan_total1.value = fmobj.daikuan_total2.value;
		fmobj.all_total1.value = fmobj.all_total2.value;
		fmobj.accrual1.value = fmobj.accrual2.value;
		fmobj.money_first1.value = fmobj.money_first2.value;
		fmobj.month1.value = fmobj.month2.value;

		fmobj.month_money1.value = fmobj.month_money2.value;
	}
}


